<!--
 * @Author: your name
 * @Date: 2022-04-08 22:36:59
 * @LastEditTime: 2022-04-18 22:56:19
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \react-antd-admin\vue-admin-template\src\views\article\publishArticle.vue
-->
<template>
  <!-- 发布资讯页面 -->
  <div>
    <div style="text-align:left;padding: 1em 2em;">
      <el-button type="text"
                 size="small"
                 @click="$router.back()">返回</el-button>
    </div>
    <el-form ref="Up_form"
             :model="form"
             label-width="60px"
             :rules="rules">
      <el-row>
        <el-col :span="16">
          <el-form-item label="标题"
                        prop="title">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-form-item label="分类"
                        prop="category_id">
            <el-select v-model="form.category_id"
                       placeholder="请选择栏目"
                       clearable>

              <el-option v-for="item in categoryData"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="封面"
                        prop="cover">
            <el-upload name="file"
                       class="avatar-uploader"
                       :action="uploadFileURL"
                       :show-file-list="false"
                       :on-success="uploadSuccess">
              <img v-if="imageUrl"
                   :src="imageUrl"
                   class="avatar">
              <i v-else
                 class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="正文"
                    prop="content">
        <tinymce v-model="form.content"
                 :height="240" />
      </el-form-item>

      <el-form-item>
        <div style="text-align:right">
          <el-button type="text"
                     @click="back ">返回</el-button>
          <el-button type="primary"
                     @click="onSubmit">{{this.btn}}</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import { getAllCategory } from '@/api/category'
import { createArticle, getArticle, updateArticle } from '@/api/article'
import { uploadFileURL } from '@/utils/config'

export default {
  name: 'TinymceDemo',
  components: { Tinymce },
  data() {
    return {
      form: {},
      categoryData: [],
      rules: {
        categoryId: [{ required: true, message: '请选择栏目', trigger: 'change' }],
        title: [{ required: true, message: '请输入文章标题', trigger: 'change' }],
        content: [{ required: true, message: '请输入文章内容', trigger: 'change' }]
      },
      uploadFileURL,
      imageUrl: '',
      id: NaN,
      btn: '发布'
    }
  },
  computed: {},
  watch: {
    'id': function (newVal) {
      if (newVal) {
        this.btn = '修改'
      }
    },
  },
  created() {
    this.id = this.$route.query.id
    this.getAllCategory()
    this.findArticleById()
  },
  mounted() { },
  methods: {
    back() {
      this.$router.go(-1)
    },
    async getAllCategory() {
      const res = await getAllCategory()
      this.categoryData = res.list
    },
    async findArticleById() {
      if (this.id) {
        const res = await getArticle({ id: this.id })
        this.form = res.data
        this.imageUrl = res.data.cover;
      }
    },
    onSubmit() {
      this.$refs.Up_form.validate(async (valid) => {
        if (valid) {
          let res;
          if (this.id) {
            res = await updateArticle({ ...this.form })
          } else {
            res = await createArticle({ ...this.form })
          }
          this.$notify.success({
            title: '成功',
            message: res.message
          })
          this.back()
        } else {
          return false
        }
      })
    },
    // 上传成功的回调函数
    uploadSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.cover = res.data.file
    },
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style scoped>
.editor-content {
  margin-top: 20px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
